//
//  WBHomeViewController.swift
//  Swift_sina
//
//  Created by TFB_WGP on 2017/8/16.
//  Copyright © 2017年 TFB_WGP. All rights reserved.
//

import UIKit
import Alamofire

//定义全局常量,私有
private let cellID = "cellID"

class WBHomeViewController: WBBaseViewController,YBPopupMenuDelegate {
    
    lazy var statusListViewModel = WBStatusViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setNavTitle()
    }
    
    //加载数据源
    override func loadData() {
        
        statusListViewModel.loadStatus(pullup: isPullUp) { (isSuccess,shouldRefresh) in
            
            //结束刷新
            self.refreshController?.endRefreshing()
            self.isPullUp = false
            if shouldRefresh {
                //刷新数据
                self.tableView?.reloadData()
            }
        }
    }
    
    @objc fileprivate func cameraBtnDidClick(){
        
        let cameraVC = WBCameraViewController()
        present(cameraVC, animated: true, completion: nil)
    }
    
    @objc fileprivate func scanBtnDidClick(sender: UIButton){
        
        YBPopupMenu.showRely(on: sender, titles: ["扫一扫","打车"], icons: ["navigationbar_pop","navigationbar_pop"], menuWidth: 140, delegate: self)
    }

}

//MARK: - 表格数据源方法
extension WBHomeViewController{
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID)
        let statusModel = statusListViewModel.statusList[indexPath.item]
        cell?.textLabel?.text = statusModel.text
        return cell!
    }
}

extension WBHomeViewController{
    
    fileprivate func setNavTitle(){
    
        let title = NetworkRequest.sharedInstance.userAccount.screen_name
        let button = WBHomeTitleBtn(title: title)
        navItem.titleView = button
    }
    
    override func setupTableView() {
        super.setupTableView()
        
        view.backgroundColor = UIColor.orange
        setLeftBarButtonItemWithSpace(arr: [UIBarButtonItem(normalImage: "navigationbar_icon_camera", highlightImage:"navigationbar_icon_camera_highlight",target: self, action: #selector(cameraBtnDidClick))])
        setRightBarButtonItemWithSpace(arr: [UIBarButtonItem(normalImage: "navigationbar_pop", highlightImage:"navigationbar_pop_highlighted",target: self, action: #selector(scanBtnDidClick(sender:)))])
        
        //注册cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
    }
    
     override func setupVisitorView(){
        super.setupVisitorView()
        
        setLeftBarButtonItemWithSpace(arr: [UIBarButtonItem(title: "注册", fontSize: btnFont, normalColor: UIColor.orange, target: self, action: #selector(registerBtnDidClick))])
        setRightBarButtonItemWithSpace(arr: [UIBarButtonItem(title: "登录", fontSize: btnFont, normalColor: UIColor.orange, target: self, action: #selector(loginBtnDidClick))])
    }
}

//MARK: - YBPopupMenu的代理方法
extension WBHomeViewController{
    //YBPopupMenu的代理方法
    func ybPopupMenuDidSelected(at index: Int, ybPopupMenu: YBPopupMenu!) {
        print(index)
    }
}
